﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public partial class admin_UKNOW_Upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.fildname.Value = Request.Params["fildname"];
            this.uptype.Value = Request.Params["uptype"];
            this.sy.Value = Request.Params["sy"];
        }
    }

    protected void upload_Click(object sender, EventArgs e)
    {
        string exts = ".bmp,.jpg,.jpge,.gif,.png,.flv";
        int maxfilesize = 2 * 1024 * 1024; //2M
        //file.PostedFile.
        string strBaseLocation = Server.MapPath("~/uploadfiles/cms/");
        if (this.uptype.Value == "file")
        {
            strBaseLocation += "file/";
            exts = ".doc,.docx,.rar,.zip,.txt,.xls,.xlsx,.flv,.mp4,.swf,.pdf,";
            maxfilesize = 100 * 1024 * 1024; //100M
        }
        else
        {
            strBaseLocation += "image/";
        }
        try
        {
            string temp_name = DateTime.Now.ToString("yyyymmddhhmmss").Trim() + file.PostedFile.ContentLength.ToString();
            string temp_ext = file.FileName.Substring(file.FileName.LastIndexOf("."), file.FileName.Length - (file.FileName.LastIndexOf(".")));

            string file_name = temp_name + temp_ext;
            if (file.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
            {
                if (file.PostedFile.ContentLength > maxfilesize)
                {
                    outText("上传文件超过最大限制!&nbsp;<a href='#' onclick='javascript:history.back();'>返回</a>");
                    return;
                }
                if (exts.IndexOf(temp_ext) == -1)
                {
                    outText("不能上传此类型文件!&nbsp;<a href='#' onclick='javascript:history.back();'>返回</a>");
                    return;
                }
                file.PostedFile.SaveAs(strBaseLocation + file_name);
                //if (this.uptype.Value != "file")
                //    outText("<img id='image' ='' src=" + "../../uploadfiles/Image/" + file_name + " /><br/>文件上传成功!&nbsp;<a href='#' onclick='javascript:history.back();'>重新上传</a><script>parent.document.forms[0]." + this.fildname.Value + ".value='" + file_name + "'</script>");
                //else
                if (this.sy.Value == "1")
                {
                    string copypath = Server.MapPath("~/uploadfiles/cms/image/") + "201116251216357730.png";
                    ImageWater_Word Myimage = new ImageWater_Word();
                    float alpha = 1F;
                    bool flag = Myimage.DrawImages(strBaseLocation + file_name, copypath, alpha, ImagePosition.RightTop, true);
                    if (flag == true)
                    {
                        outText("文件上传成功!&nbsp;<a href='#' onclick='javascript:history.back();'>重新上传</a><script>parent.document.forms[0]." + this.fildname.Value + ".value='" + file_name + "'</script>");
                        return;
                    }
                    else
                    {
                        outText("文件上传失败123!&nbsp;<a href='#' onclick='javascript:history.back();'>重新上传</a><script>parent.document.forms[0]." + this.fildname.Value + ".value='" + file_name + "'</script>");
                        return;
                    }
                }
                else
                {
                    outText("文件上传成功!&nbsp;<a href='#' onclick='javascript:history.back();'>重新上传</a><script>parent.document.forms[0]." + this.fildname.Value + ".value='" + file_name + "'</script>");
                    return;
                }
            }
        }
        catch (Exception er)
        {
            outText("上传文件失败!" + er.ToString() + "&nbsp;<a href='#' onclick='javascript:history.back();'>返回</a>");
        }

    }

    protected void outText(string str)
    {

        string strHtml = "<style type=\"text/css\">";
        strHtml += "body{";
        strHtml += "font-size: 12px;margin-left: 0px;margin-right: 0px;margin-bottom: 0px;";
        strHtml += "}td{font-size:12px;}";
        strHtml += "</style>";

        strHtml += "<table border=\"0\" width=\"409\"  cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"0\"><tr><td>";

        strHtml += str;

        strHtml += "</td></tr></table>";

        Response.Write(strHtml);

    }
}

